package com.goktech.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.goktech.domain.Student;
import com.goktech.domain.User;
import com.goktech.service.StudentService;

@Controller
@RequestMapping("admin")
public class AdminController {
	
	@Autowired
	private StudentService studentService;
	
	@RequestMapping("")
	public ModelAndView studentAdd() {
		return new ModelAndView("admin/student");
	}
	
	@RequestMapping("/doAddStudent")
	public ModelAndView doAdd(@ModelAttribute Student student) {
		studentService.add(student);
		return new ModelAndView("admin/student");
	}
	
	@RequestMapping("/doSelectAllStudent")
	public ModelAndView doSelectAll(Model model,@ModelAttribute Student student) {
		List<Student> list =studentService.selectAll();
		System.out.println(list.size());
		model.addAttribute("message", list.size());
		model.addAttribute("list",list);
		return new ModelAndView("admin/student");
	}
	
	@RequestMapping(value="/delectStudent", method = RequestMethod.GET)
	public ModelAndView doDelect(@RequestParam String id) {
		Integer intId=Integer.parseInt(id);
		studentService.delete(intId);
		return new ModelAndView("admin/student");
	}
	
	@RequestMapping(value = {"/user/{userId}/abc/{userName}"})
	public String user(Model model,@PathVariable String userId,@PathVariable String userName) {
		
		model.addAttribute("message", "user");
		return "user/user";
	}
	
	/**
	 * @param model
	 * @param userId
	 * @param userName
	 * @return
	 */
	@RequestMapping(value = {"/user/doAdd"},method= {RequestMethod.GET,RequestMethod.POST})
	public String addUser(Model model,@ModelAttribute User user,
			HttpServletRequest req,HttpServletResponse res) {
		
		model.addAttribute("message", "user");
		return "user/user";
	}
	
	@RequestMapping("/list")
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView("user/user");
		mav.addObject("message", "tom");
		return mav;
	}
	

}